<?php
include_once( 'saetv2.ex.class.php' );

session_start();

$tClientV2;

if (isset($_SESSION['saeTClientV2'])) {
	$tClientV2 = $_SESSION['saeTClientV2'];
} else {
	header('Location: index.php');

	exit;
}

$action = $_REQUEST['action'];

if ('queryStatuses' == $action) {
	$userId = NULL;

	if (isset($_REQUEST['userId'])) {
		$userId = $_REQUEST['userId'];
	}

	$page = $_REQUEST['page'];
	$count = $_REQUEST['count'];

	$response = queryStatuses($tClientV2, $userId, $page, $count);

	echo json_encode($response);
} else if ('deleteStatuses' == $action) {
	$statusesIds = NULL;
	
	if (isset($_REQUEST['statusesIds'])) {
		$statusesIds = explode(',', $_REQUEST['statusesIds']);
	}
	
	$response = deleteStatuses($tClientV2, $statusesIds);
	
	echo join(',', $response);
}

//Query Statuses
function queryStatuses($tClientV2, $userId, $page, $count) {
	$response = $tClientV2->user_timeline_by_id($userId, $page, $count);
	
	$statuses = array();
	$totalNumber = 0;
	
	if (!isset($response['error']) && !isset($response['error_code'])) {
		for ($i = 0; $i < count($response['statuses']); $i++) {
			$status = $response['statuses'][$i];
		
			$id = $status['idstr'];
			$text = $status['text'];
			$thumbnailPic = NULL;
		
			if (isset($status['thumbnail_pic'])) {
				$thumbnailPic = $status['thumbnail_pic'];
			}
				
			$statuses[$i] = array(
					'id' => $id,
					'text' => $text,
					'thumbnailPic' => $thumbnailPic,
			);
		}
		
		$totalNumber = $response['total_number'];
	}
	
	return array(
			'statuses' => $statuses,
			'totalNumber' => $totalNumber,
	);
}

//Delete Statuses
function deleteStatuses($tClientV2, $statusesIds) {
	$deletedStatusesIds = array();

	for ($i = 0; $i < count($statusesIds); $i++) {
		$statusId = $statusesIds[$i];

		$response = $tClientV2->delete($statusId);

		if (!isset($response['error']) && !isset($response['error_code'])) {
			array_push($deletedStatusesIds, $response['idstr']);
		}
	}

	return $deletedStatusesIds;
}
?>